#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent),
      isBan(false),
      isLogin(true)
{
    demoCount = 0;

    // 设置窗口大小
    resize(1600, 900);
    setWindowTitle("学海通航");
    setMinimumSize(1280, 720); // 设置最小窗口大小

    // 主布局（顶层布局）
    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    // 左侧菜单栏
    QWidget *leftPanel = new QWidget;
    QVBoxLayout *leftLayout = new QVBoxLayout(leftPanel);
    leftLayout->setContentsMargins(0, 0, 0, 0);

    // 上部按钮区域
    QVBoxLayout *upperButtons = new QVBoxLayout;

    // 下部按钮区域
    QVBoxLayout *lowerButtons = new QVBoxLayout;

    // 添加伸缩空间分割上下区域
    leftLayout->addLayout(upperButtons);
    leftLayout->addStretch();
    leftLayout->addLayout(lowerButtons);

    // 创建按钮
    auto createButton = [](const QString &iconPath, const QString &text)
    {
        QPushButton *btn = new QPushButton;
        btn->setIcon(QIcon(iconPath));
        btn->setIconSize(QSize(50, 50));
        btn->setToolTip(text);
        btn->setStyleSheet("QPushButton {"
                           "border-radius: 10px;"
                           "background: #f0f0f0;"
                           "}"
                           "QPushButton:hover { background: #e0e0e0; }");
        return btn;
    };

    // 添加上部按钮
    QPushButton *homeBtn = createButton(":/uiIcon/resoures/uiIcon/home.png", "主页");
    upperButtons->addWidget(homeBtn);

    QPushButton *followedPostsBtn = createButton(":/uiIcon/resoures/uiIcon/followedPosts.png", "关注动态");
    upperButtons->addWidget(followedPostsBtn);

    // 添加下部按钮
    QPushButton *messageBtn = createButton(":/uiIcon/resoures/uiIcon/message.png", "消息");
    lowerButtons->addWidget(messageBtn);

    QPushButton *createBtn = createButton(":/uiIcon/resoures/uiIcon/create.png", "创作中心");
    lowerButtons->addWidget(createBtn);

    QPushButton *myBtn = createButton(":/uiIcon/resoures/uiIcon/my.png", "我的");
    lowerButtons->addWidget(myBtn);

    // 内容区域
    contentArea = new QStackedWidget(this);

    // 主布局添加部件
    mainLayout->addWidget(leftPanel);
    mainLayout->addWidget(contentArea);

    // 设置布局
    setLayout(mainLayout);

    // 添加页面到堆栈
    homePage = new HomePage(this);
    followedPostsPage = new BlogListWidget(this);
    messagePage = new CommentListWidget(this);
    createPage = new CreatePage(this);
    myPage = new MyPage(this);
    blogDetailsPage = new BlogDetailsPage(this);
    banPage = new BanPage(this);
    loginPage = new LoginPage(this);
    registerPage = new RegisterPage(this);

    // 创建 QScrollArea 并设置内容
    messageScrollArea = new QScrollArea(this);
    messageScrollArea->setWidgetResizable(true); // 自动调整大小
    messageScrollArea->setWidget(messagePage);

    contentArea->addWidget(homePage);          // 主页
    contentArea->addWidget(followedPostsPage); // 关注动态
    contentArea->addWidget(messageScrollArea); // 消息
    contentArea->addWidget(createPage);        // 创作中心
    contentArea->addWidget(myPage);            // 我的
    contentArea->addWidget(blogDetailsPage);   // 文章详情页
    contentArea->addWidget(banPage);           // 封禁
    contentArea->addWidget(loginPage);         // 登录
    contentArea->addWidget(registerPage);      // 注册

    // 连接按钮信号槽
    connect(homeBtn, &QPushButton::clicked, [this]()
            { 
                demoCount = 0;
                contentArea->setCurrentWidget(homePage); });

    connect(followedPostsBtn, &QPushButton::clicked, [this]()
            {
                demoCount = 0;
                if (isLogin)
                {
                    contentArea->setCurrentWidget(followedPostsPage);
                }
                else
                {
                    contentArea->setCurrentWidget(loginPage); 
                } });

    connect(messageBtn, &QPushButton::clicked, [this]()
            {
                demoCount = 0;
                if (isLogin)
                {
                    contentArea->setCurrentWidget(messageScrollArea);
                }
                else
                {
                    contentArea->setCurrentWidget(loginPage); 
                } });
    connect(createBtn, &QPushButton::clicked, [this]()
            {
                if (isLogin)
                {
                    if (isUserBanned())
                    {
                        contentArea->setCurrentWidget(banPage);
                    }
                    else
                    {
                        contentArea->setCurrentWidget(createPage); 
                    }
                }
                else
                {
                    contentArea->setCurrentWidget(loginPage);
                } });
    connect(myBtn, &QPushButton::clicked, [this]()
            {
                demoCount = 0;
                if (isLogin)
                {
                    contentArea->setCurrentWidget(myPage);
                }
                else
                {
                    contentArea->setCurrentWidget(loginPage); 
                } });
    connect(createPage, &CreatePage::previewSignal, this, [this](const QString &title, const QString &htmlContent)
            { 
                BlogDetail blogDetail;
                blogDetail.title = title;
                blogDetail.time = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm");
                blogDetail.readCount = 0;
                blogDetail.category = "计算机";
                blogDetail.authorAvatar = QPixmap(":/test/resoures/test/avatar1.png");
                blogDetail.authorName = "脸滚键盘1201";
                blogDetail.htmlContent = htmlContent;
                blogDetail.likeCount = 0;
                blogDetail.commentCount = 0;
                blogDetail.favoriteCount = 0;
                blogDetailsPage->clearComment();
                blogDetailsPage->setByDataModel(blogDetail);
                contentArea->setCurrentWidget(blogDetailsPage); });

    connect(myPage, &MyPage::logoutSignal, [this]()
            {   isLogin = false;
                contentArea->setCurrentWidget(loginPage); });
    connect(loginPage, &LoginPage::loginAttempted, [this](const QString &account, const QString &password)
            { 
                qDebug() << "登陆账号：" << account;
                qDebug() << "密码：" << password;
                isLogin = true;
                contentArea->setCurrentWidget(myPage); });

    connect(loginPage, &LoginPage::goToRegister, [this]()
            { contentArea->setCurrentWidget(registerPage); });

    connect(registerPage, &RegisterPage::registerAttempted, [this](const QString &account, const QString &password)
            { 
                qDebug() << "登陆账号：" << account;
                qDebug() << "密码：" << password;
                isLogin = true;
                contentArea->setCurrentWidget(myPage); });

    connect(registerPage, &RegisterPage::goToLogin, [this]()
            { contentArea->setCurrentWidget(loginPage); });

    connect(homePage, &HomePage::openDetailPage, [this]()
            {   
                blogDetailsPage->setByDataModel(DataAccess::getBlogDetail());
                contentArea->setCurrentWidget(blogDetailsPage); });

    connect(blogDetailsPage, &BlogDetailsPage::authorProfileRequested, [this]()
            { contentArea->setCurrentWidget(myPage); });

    connect(blogDetailsPage, &BlogDetailsPage::editRequested, [this](QString title, QString htmlContent)
            { 
                createPage->setTitle(title);
                createPage->setHtmlContent(htmlContent);
                contentArea->setCurrentWidget(createPage); });

    followedPostsPage->load(DataAccess::getBlogListByFollowed());
}

bool MainWindow::isUserBanned()
{
    demoCount++;
    if (demoCount > 2)
    {
        isBan = true;
    }
    else
    {
        isBan = false;
    }
    return isBan;
}
